package models

import (
	"gorm.io/gorm"
	"month_zg5/book_srv/common/global"
)

// 用户表
type User struct {
	gorm.Model
	Username string      `gorm:"type:varchar(11);not null;comment:账号"`
	Password string      `gorm:"type:varchar(100);not null;comment:密码"`
	Email    string      `gorm:"type:varchar(30);not null;comment:邮箱"`
	Phone    string      `gorm:"type:varchar(11);not null;comment:手机号码"`
	VipId    int         `gorm:"type:int(2);not null;default:0;comment:会员等级"`
	Status   int         `gorm:"type:tinyint(1);check:status IN (0,1);not null;comment:状态"`
	ReadList []*ReadList `gorm:"foreignKey:Userid" json:"ReadList"`
}

// 阅读记录表
type ReadList struct {
	gorm.Model
	Userid int
	Title  string `gorm:"type:varchar(255);not null;comment:内容"`
}

// 书架表
type BookShelf struct {
	gorm.Model
	UserId uint `gorm:"type:int(2);not null;comment:用户id"`
	BookId uint `gorm:"type:int(2);not null;comment:书本id"`
}

// 图书表
type Book struct {
	gorm.Model
	Title            string    `gorm:"type:varchar(30);not null;index;comment:书本名称" json:"title"`
	BookType         uint      `gorm:"type:int(2);not null;comment:书籍分类" json:"book_type"`
	Author           string    `gorm:"type:varchar(50);not null;comment:作者" json:"author"`
	Content          string    `gorm:"type:TEXT;not null;comment:图书简介" json:"content"`
	Common           []*Common `gorm:"foreignKey:BookId" json:"common"`
	NumberOfComments int       `gorm:"type:int(20);not null;comment:浏览量" json:"number_of_comments"`
	Readings         int       `gorm:"type:int(20);not null;comment:阅读量" json:"readings"`
}

// 评论表
type Common struct {
	gorm.Model
	UserId  uint      `gorm:"type:int(2);not null;comment:用户id"`
	BookId  uint      `gorm:"type:int(2);not null;comment:书本id"`
	Content string    `gorm:"type:TEXT;not null;comment:评论内容"`
	Pid     int       `gorm:"type:int(2);comment:父级id"`
	Common  []*Common `gorm:"foreignKey:Pid" json:"common"`
}

// 分类表
type BookType struct {
	Id       uint        `gorm:"primarykey"`
	Title    string      `gorm:"type:varchar(20);not null;comment:分类名称"`
	Pid      uint        `gorm:"type:int(2);comment:父级id"`
	BookType []*BookType `gorm:"foreignKey:Pid" json:"book_type"`
}

func InitMigrate() {
	err := global.DB.AutoMigrate(&User{}, &Book{}, &BookShelf{}, &BookType{}, &Common{}, &ReadList{})
	if err != nil {
		panic(err)
	}
}
